﻿using CL.Business.BLL;
using CL.Business.Entity;
using CL.Game.BLL;
using CL.Tools.Common;
using CL.UnionWeb.union;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.UnionWeb
{
    public partial class Register : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if("POST" == Request.RequestType)
            {
                var userInfo = new AccountEntity();
                userInfo.AccountCode = Request["useraccount"].ToString();
                userInfo.AccountPwd = Request["userpwd"].ToString();
                userInfo.FullName = Request["username"].ToString();
                userInfo.IdentityID = Request["userIDcard"].ToString();
                userInfo.BankName = Request["bankname"].ToString();
                userInfo.BankCard = Request["bankcard"].ToString();
                userInfo.UserMobile = Request["userPhone"].ToString();
                string vCode = Request["phoneCheck"].ToString();

                if (string.IsNullOrEmpty(userInfo.AccountCode) ||
                  string.IsNullOrEmpty(userInfo.AccountPwd) ||
                  string.IsNullOrEmpty(userInfo.FullName) ||
                  string.IsNullOrEmpty(userInfo.IdentityID) ||
                  string.IsNullOrEmpty(userInfo.BankName) ||
                  string.IsNullOrEmpty(userInfo.BankCard) ||
                  string.IsNullOrEmpty(userInfo.UserMobile)
                  )
                {
                      SendMsgToView("参数错误, 请重新注册!  ", "/Register.aspx");
                }

                var bll = new AccountBLL();
                var ret = bll.MobileRegister(userInfo, vCode);
                if (0 == ret.Code)
                {
                    var entity = bll.QueryEntityByMoblie(userInfo.UserMobile);
                    if(null != entity)
                    {
                        //记录
                        ShareRecordEntity shareRecord = new ShareRecordEntity();
                        shareRecord.AccountID = entity.AccountID;
                        shareRecord.ShareTime = DateTime.Now;
                        shareRecord.ShareType = 4;

                        var appObj = Request["appID"];
                        if (null != appObj)
                        {
                            string appAccountID = appObj.ToString();
                            if (!string.IsNullOrEmpty(appAccountID))
                            {
                                //关联代理
                                var menBll = new UserToAgentMembershipBLL();
                                var menEntity = new UserToAgentMembershipEntity();
                                shareRecord.UserID = menEntity.UserID = int.Parse(appAccountID);
                                menEntity.AccountID = entity.AccountID;
                                menEntity.ShipTime = DateTime.Now;
                                menBll.AddEntity(menEntity);
                            }
                        }
                        new ShareRecordBLL().InsertEntity(shareRecord);

                        //注册成功后直接跳转到"我的信息"
                        Session[CLKeys.SESSION_ADMIN_INFO] = entity;
                        Session.Timeout = 30;

                        SendMsgToView("注册成功!  ", "/union/MyAccount.aspx");
                    }
                    else
                    {
                        SendMsgToView("获取用户信息失败!  ", "/Register.aspx");
                    }
                }
                else
                {
                    SendMsgToView("注册失败!  ", "/Register.aspx");
                }
            }
        }

        protected void SendMsgToView(string msg, string url = null)
        {
            if (string.IsNullOrEmpty(url))
                Response.Write("<script >alert(\"" + msg + "\");</script>");
            else
                Response.Write("<script >alert(\"" + msg + "\");location.href='" + url + "';</script>");
            Response.End();
        }
    }
}